package com.longnuoqi.mydoctor.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.longnuoqi.mydoctor.activity.DoctorAskMoreActivity;
import com.longnuoqi.mydoctor.adapter.DoctorGridAdapter;
import com.longnuoqi.mydoctor.utils.ViewPagerUtil;
import com.yunhe.mydoctor.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Lenovo on 2015/12/26.
 */
public class DoctorFragment extends Fragment {
    private EditText searchText;
    private ImageView searchBtn;

    private ViewPager viewPager;
    private final static int[] pics = {R.mipmap.banner3, R.mipmap.banner4}; //轮播页面图片

    private GridView gridView;
    private DoctorGridAdapter doctorGridAdapter;
    private final static int[] gvImg = {R.mipmap.gv_tiwen, R.mipmap.gv_wenzhen, R.mipmap.gv_wenyao, R.mipmap.gv_guahao};
    private final static String[] gvTxt = {"我要提问", "留言问诊", "寻医问药", "预约挂号"};

    private ImageView more;
    private RelativeLayout fuke;
    private RelativeLayout erke;
    private RelativeLayout nanke;
    private RelativeLayout pifu;
    private RelativeLayout waike;

    private TextView fukeTxt;
    private TextView erkeTxt;
    private TextView nankeTxt;
    private TextView pifuTxt;
    private TextView waikeTxt;
    private View[] all;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.doctor_layout, container, false);
        initView(view);
        initViewPager(view);
        initGridView(view);
        initFragment(view);
        return view;
    }

    private void initView(View view) {
        searchText = (EditText) view.findViewById(R.id.doctor_search_text);
        searchBtn = (ImageView) view.findViewById(R.id.doctor_search_btn);
        searchBtn.setOnClickListener(onClickListener);
    }

    private void initViewPager(View view) {
        viewPager = (ViewPager) view.findViewById(R.id.doctor_viewpager);
        new ViewPagerUtil().viewPagerUtil(getContext(), view, viewPager, pics);
    }

    private void initGridView(View view) {
        gridView = (GridView) view.findViewById(R.id.doctor_gridview);
        List list = new ArrayList<>();
        for (int i = 0; i < gvImg.length; i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("img", gvImg[i]);
            map.put("name", gvTxt[i]);
            list.add(map);
        }
        doctorGridAdapter = new DoctorGridAdapter(getActivity(), list);
        gridView.setAdapter(doctorGridAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "position:" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initFragment(View view) {
        more = (ImageView) view.findViewById(R.id.doctor_more_btn);

        fuke = (RelativeLayout) view.findViewById(R.id.doctor_fuke_rl);
        erke = (RelativeLayout) view.findViewById(R.id.doctor_erke_rl);
        nanke = (RelativeLayout) view.findViewById(R.id.doctor_nanke_rl);
        pifu = (RelativeLayout) view.findViewById(R.id.doctor_pifu_rl);
        waike = (RelativeLayout) view.findViewById(R.id.doctor_waike_rl);

        fukeTxt = (TextView) view.findViewById(R.id.doctor_fuke_text);
        erkeTxt = (TextView) view.findViewById(R.id.doctor_erke_text);
        nankeTxt = (TextView) view.findViewById(R.id.doctor_nanke_text);
        pifuTxt = (TextView) view.findViewById(R.id.doctor_pifu_text);
        waikeTxt = (TextView) view.findViewById(R.id.doctor_waike_text);


        more.setOnClickListener(onClickListener);
        fuke.setOnClickListener(onClickListener);
        erke.setOnClickListener(onClickListener);
        nanke.setOnClickListener(onClickListener);
        pifu.setOnClickListener(onClickListener);
        waike.setOnClickListener(onClickListener);

        all = new View[]{fuke, fukeTxt, erke, erkeTxt, nanke, nankeTxt, pifu, pifuTxt, waike, waikeTxt};

        fuke.setEnabled(false);
        fukeTxt.setEnabled(false);
        Bundle bundle = new Bundle();
        bundle.putInt("id", 1);
        showFragment(bundle);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle bundle = null;
            View linear = null;
            View text = null;
            switch (v.getId()) {
                case R.id.doctor_search_btn:
                    Toast.makeText(getActivity(), "搜索相关内容", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.doctor_more_btn:
                    Intent intent = new Intent(getActivity(), DoctorAskMoreActivity.class);
                    startActivityForResult(intent, 0);
                    break;
                case R.id.doctor_fuke_rl:
                    linear = fuke;
                    text = fukeTxt;
                    showColor(linear, text);
                    v.setBackgroundResource(R.drawable.doctor_tab_selector);
                    bundle = new Bundle();
                    bundle.putInt("id", 1);
                    showFragment(bundle);
                    break;
                case R.id.doctor_erke_rl:
                    linear = erke;
                    text = erkeTxt;
                    showColor(linear, text);
                    v.setBackgroundResource(R.drawable.doctor_tab_selector);
                    bundle = new Bundle();
                    bundle.putInt("id", 2);
                    showFragment(bundle);
                    break;
                case R.id.doctor_nanke_rl:
                    linear = nanke;
                    text = nankeTxt;
                    showColor(linear, text);
                    v.setBackgroundResource(R.drawable.doctor_tab_selector);
                    bundle = new Bundle();
                    bundle.putInt("id", 4);
                    showFragment(bundle);
                    break;
                case R.id.doctor_pifu_rl:
                    linear = pifu;
                    text = pifuTxt;
                    showColor(linear, text);
                    v.setBackgroundResource(R.drawable.doctor_tab_selector);
                    bundle = new Bundle();
                    bundle.putInt("id", 5);
                    showFragment(bundle);
                    break;
                case R.id.doctor_waike_rl:
                    linear = waike;
                    text = waikeTxt;
                    showColor(linear, text);
                    v.setBackgroundResource(R.drawable.doctor_tab_selector);
                    bundle = new Bundle();
                    bundle.putInt("id", 6);
                    showFragment(bundle);
                    break;
                default:
                    break;
            }

        }
    };

    private void showColor(View linear, View text) {
        for (int i = 0; i < all.length; i++) {
            all[i].setEnabled(true);
        }
        linear.setEnabled(false);
        text.setEnabled(false);
    }

    private void showFragment(Bundle bundle) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        Fragment fragment = new DoctorListFragment();
        fragment.setArguments(bundle);
        transaction.replace(R.id.doctor_framelayout, fragment);
        transaction.commit();
    }


}
